package cn.wolfcode.wolf2w.conteoller;

import cn.wolfcode.wolf2w.annontation.RequireLogin;
import cn.wolfcode.wolf2w.annontation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService ;
    @Autowired
    private IStrategyThemeService strategyThemeService ;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyRedisService strategyRedisService;
    @GetMapping("/query")
    public JsonResult list(StrategyQuery qo){
        Page<Strategy> page = strategyService.query(qo);
        //System.out.println(page);
        return JsonResult.success(page);
    }

    @GetMapping("/themes")
    public JsonResult getThemes(){
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }
    @GetMapping("/detail")
    public JsonResult detail(String id){
        Strategy strategy = strategyService.get(id);
        strategyRedisService.increaseViewnum(id,1);
        return JsonResult.success(strategy);
    }
    @RequireLogin
    @PostMapping("/addComment")
    public JsonResult addComment(StrategyComment comment, @UserParam UserInfo userInfo){
        BeanUtils.copyProperties(userInfo,comment );
        comment.setUserId(userInfo.getId());
        strategyCommentService.addComment(comment);
        strategyRedisService.increaseReply(comment.getStrategyId(),1);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    public JsonResult comments(StrategyCommentQuery qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }
    @PostMapping("/commentThumb")
    public JsonResult commentThumb(String cid ,@UserParam UserInfo userInfo){
        String uid = userInfo.getId();
        strategyCommentService.commentThumb(cid,uid);
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public JsonResult statisVo(String sid ){
        StrategyStatisVO vo = strategyRedisService.queryStatisVo(sid);
        return JsonResult.success(vo);
    }

    @RequireLogin
    @PostMapping("/favor")
    public JsonResult favor(String sid,@UserParam UserInfo userInfo ){
        boolean ret = strategyRedisService.favor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")
    public JsonResult strategyThumbup(String sid,@UserParam UserInfo userInfo ){
        boolean ret = strategyRedisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(ret);
    }


}
